package me.xuym.md2htm.extension;

import com.vladsch.flexmark.ast.Node;
import com.vladsch.flexmark.html.LinkResolver;
import com.vladsch.flexmark.html.renderer.LinkResolverContext;
import com.vladsch.flexmark.html.renderer.ResolvedLink;

/**
 * Created by broche on 11/12/2017.
 */
public class GitlabLinkResolver implements LinkResolver {
    @Override
    public ResolvedLink resolveLink(Node node, LinkResolverContext context, ResolvedLink link) {
        String url = link.getUrl();
        if (url.contains("#")) {
            String[] strs = url.split("#");
            if (strs[0].length() > 0) {
                if (!strs[0].contains(".")) {
                    url = strs[0] + ".htm#" + strs[1];
                } else {
                    // do nothing
                }
            } else {
                // do nothing
            }
        } else {
            if (!url.contains(".")) {
                url = url + ".htm";
            }
        }
        return link.withUrl(url);
    }
}
